package com.qianying.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianying.comment.CommonResult;
import com.qianying.entity.SysUser;
import com.qianying.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/sysUser")
@Tag(name = "用户管理", description = "系统用户的CRUD及登录注册接口")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    // 其他原有接口保持不变...

    /**
     * 按角色ID、用户名、手机号、状态多条件分页查询用户
     */
    @PostMapping("/getUserPageByRoleId")
    @Operation(
            summary = "多条件分页查询用户",
            description = "支持按角色ID、用户名（模糊）、手机号（模糊）、用户状态筛选，0表示未分配角色，不传表示不限制该条件",
            parameters = {
                    @Parameter(name = "current", description = "页码，默认1", example = "1", required = false),
                    @Parameter(name = "size", description = "每页条数，默认10", example = "10", required = false),
                    @Parameter(name = "roleId", description = "角色ID（0表示未分配角色，null表示不限制）", example = "1", required = false),
                    @Parameter(name = "username", description = "用户名（模糊查询）", example = "admin", required = false),
                    @Parameter(name = "phone", description = "手机号（模糊查询）", example = "138", required = false),
                    @Parameter(name = "enabled", description = "用户状态（0-无效，1-有效，null表示不限制）", example = "1", required = false)
            }
    )
    public CommonResult<IPage<SysUser>> getUserPageByRoleId(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestBody(required = false) SysUser queryParam) {

        Page<SysUser> page = new Page<>(current, size);
        // 处理空参数，避免空指针
        if (queryParam == null) {
            queryParam = new SysUser();
        }
        return sysUserService.getUserPageByRoleId(page, queryParam);
    }

    /**
     * 更新用户信息（支持部分字段更新）
     */
    @PostMapping("/updateUserAllInfo")
    @Operation(
            summary = "更新用户所有信息",
            description = "支持更新用户名、手机号、角色ID、状态等信息，只更新非空字段，ID为必填项",
            parameters = {
                    @Parameter(name = "id", description = "用户ID（必填）", example = "1", required = true),
                    @Parameter(name = "username", description = "用户名（可选）", example = "newadmin"),
                    @Parameter(name = "phone", description = "手机号（可选）", example = "13900139000"),
                    @Parameter(name = "roleId", description = "角色ID（可选，0表示未分配）", example = "2"),
                    @Parameter(name = "enabled", description = "用户状态（可选，0-无效，1-有效）", example = "1"),
                    @Parameter(name = "anotherName", description = "用户别名", example = "张三"),
                    @Parameter(name = "des", description = "用户简介(描述)", example = "张三是个人才")
            }
    )
    public CommonResult<SysUser> updateUserAllInfo(@RequestBody SysUser user) {
        return sysUserService.updateUserInfo(user, false);
    }

    @PostMapping("/updateUserNameByUserSelf")
    @Operation(
            summary = "更新用户所有信息(用户名和电话)",
            description = "支持更新用户名、手机号、角色ID、状态等信息，只更新非空字段，ID为必填项",
            parameters = {
                    @Parameter(name = "id", description = "用户ID（必填）", example = "1", required = true),
                    @Parameter(name = "username", description = "用户名（可选）", example = "newadmin"),
                    @Parameter(name = "phone", description = "手机号（可选）", example = "13900139000"),
                    @Parameter(name = "roleId", description = "角色ID（可选，0表示未分配）", example = "2"),
                    @Parameter(name = "enabled", description = "用户状态（可选，0-无效，1-有效）", example = "1"),
                    @Parameter(name = "anotherName", description = "用户别名", example = "张三"),
                    @Parameter(name = "des", description = "用户简介(描述)", example = "张三是个人才")
            }
    )
    public CommonResult<SysUser> updateUserNameByUserSelf(@RequestBody SysUser user) {
        user.setRoleId(null);
        return sysUserService.updateUserInfo(user, true);
    }
}